package ru.jkff.jalgebra.monoid;

import static ru.jkff.jalgebra.utils.CollectionFactory.newList;
import static ru.jkff.jalgebra.utils.CollectionFactory.newUnorderedSet;

import java.util.List;
import java.util.Set;

/**
 * User: jkff
 * Date: Aug 22, 2009
 * Time: 1:05:09 PM
 */
public abstract class Aggregates {
    public static <T> Aggregate<T, List<T>> collectIntoList() {
        return new Aggregate<T, List<T>>() {
            public List<T> freshUnit()                          { return newList(); } 
            public void    pushInto(T t, List<T> ts)            { ts.add(t);        }
            public void    mergeInto(List<T> src, List<T> dest) { dest.addAll(src); }
        };
    }
    public static <T> Aggregate<T, Set<T>> collectIntoSet() {
        return new Aggregate<T, Set<T>>() {
            public Set<T> freshUnit()                        { return newUnorderedSet(); } 
            public void   pushInto(T t, Set<T> ts)           { ts.add(t);                }
            public void   mergeInto(Set<T> src, Set<T> dest) { dest.addAll(src);         }
        };
    }
}
